package zheezes.eisp.collect.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import zheezes.eisp.data.orm.CollectTask;
import zheezes.eisp.data.orm.ResMeter;
import zheezes.util.ArrayUtil;
import zheezes.util.DateFormatUtil;


public abstract class VipFrozenCollectTaskHandler extends CollectTaskHandler {
	private static Logger logger = Logger.getLogger(VipFrozenCollectTaskHandler.class);

	private List<ResMeter> list = new ArrayList<ResMeter>();

	protected List<ResMeter> query() {
		list.clear();
		list.addAll(dataAccessor.queryNoHourFrznVipMeter(task.getConId(), task.getDataDate()));
		logger.debug(String.format(
				"total %d vip meters not collected at %s", 
				list.size(), DateFormatUtil.getDateFmt().format(task.getDataDate())));

		return list;
	}

	protected ResMeter findBySeq(int seq) {
		for (ResMeter meter : list) {
			if (meter.getSeq().intValue() == seq) {
				return meter;
			}
		}
		return null;
	}

	protected ResMeter findByNo(byte[] no) {
		for (ResMeter meter : list) {
			if (ArrayUtil.sprintf(no, "%02X").equals(meter.getNo())) {
				return meter;
			}
		}
		return null;
	}
	
	protected void refreshTaskStat() {
		task.setTotalNum(dataAccessor.countVipMeterByCon(task.getConId()));
		task.setCollectedNum(dataAccessor.countVipMeterWithDailyFrznByCon(task.getConId(), task.getDataDate()));
		task.setStatus(task.getTotalNum().equals(task.getCollectedNum()) ? CollectTask.STATUS_FINISH : CollectTask.STATUS_QUEUE);
		// task.setLastRunDate(Calendar.getInstance().getTime());
		dataAccessor.updateCollectTask(task);
	}
}